package com.ming.controller;

import com.ming.anno.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:何元才
 * @Desc:
 * @data:2021-03-31 21:51
 */
@RestController
@Api(tags = "登出管理接口")
public class LogOutController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 登陆操作
     *
     * @return
     */
    @PostMapping("sys/logout")
    @ApiOperation("登出操作")
//    @ApiImplicitParam(name = "token", value = "jwt_token", required = false, paramType = "header", dataTypeClass = String.class)
    @Log(operation = "登出的操作")
     public ResponseEntity<String> logout(){
         //拿到当前的jwt 从redis删掉就可以了
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        // 这个token是没有bearer
        String tokenValue = details.getTokenValue();
        Boolean flag = redisTemplate.delete("oauth:jwt:" + tokenValue);
        System.out.println(false);
        return ResponseEntity.ok("登出成功");

    }
}
